package com.yikaiye.android.yikaiye.ui.activity.mine;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityCompanyDetailsBinding;
import com.yikaiye.android.yikaiye.model.TabModel;
import com.yikaiye.android.yikaiye.ui.activity.mine.viewmodel.CompanyDetailsViewModel;
import com.yikaiye.android.yikaiye.ui.fragment.CompanyGsInfoFragment;
import com.yikaiye.android.yikaiye.ui.fragment.CompanyInvoiceInfoFragment;
import com.yikaiye.android.yikaiye.ui.fragment.CompanyServiceProgressFragment;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.ToastHelper;
import com.yikaiye.android.yikaiye.utils.UiUtils;

import java.util.ArrayList;

/**
 * 企业详情
 */
public class CompanyDetailsActivity extends BaseActivity<ActivityCompanyDetailsBinding, CompanyDetailsViewModel> {

    private final String[] mTitles = new String[3];
    private final int[] mIconSelectIds = {R.mipmap.icon_gsxx_sel, R.mipmap.icon_service_progress_sel, R.mipmap.icon_kp_sel};
    private final int[] mIconUnselectIds = {R.mipmap.iccon_gsxx, R.mipmap.icon_service_progress, R.mipmap.icon_kp};
    private final ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private final ArrayList<Fragment> fragments = new ArrayList<>();
    /**
     * tab默认选择的位置
     */
    private int tabPos;

    @Override
    protected CompanyDetailsViewModel bindModel() {
        return getViewModel(this, CompanyDetailsViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.company_details));
        mTitles[0] = getString(R.string.gsxx);
        mTitles[1] = getString(R.string.fwjd);
        mTitles[2] = getString(R.string.kpnr);
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabModel(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        if (getIntent() != null) {
            String creditNo = getIntent().getStringExtra(Constant.CREDIT_NO_KEY);
            tabPos = getIntent().getIntExtra(Constant.TAG_KEY, 0);
            viewModel.getCompanyDetail(creditNo);
        }

        // 第二种
//        binding.tabLayout.setTabData(mTabEntities, this, R.id.fl_change, fragments);
    }

    @Override
    protected void initClick() {
        viewModel.companyDetailsModelMutableLiveData.observe(this, companyDetailsModel -> {
            if (companyDetailsModel != null) {
                binding.companyNameTv.setText(CommonUtil.checkText(companyDetailsModel.getCompanyName()));
                // 企业状态码（1：存续；2：注销；3：吊销；4：撤销，5：迁出，6：设立中，7：清算中，8：停业，9：其他）
                binding.companyStatusTv.setText(UiUtils.getManagementStatus(companyDetailsModel.getNewStatusCode()));
                UiUtils.setManagementStatusColor(context, binding.companyStatusTv, companyDetailsModel.getNewStatusCode());
                // 服务状态 1 服务中 2已结束
                binding.serviceStatusTv.setText(companyDetailsModel.getServiceStatus() == 1 ? getString(R.string.in_service) : getString(R.string.ended));
                binding.serviceStatusTv.setTextColor(ContextCompat.getColor(context, companyDetailsModel.getServiceStatus() == 1 ? R.color.color_2c7df1 : R.color.color_9b9b9b));
                binding.serviceStatusTv.setBackgroundResource(companyDetailsModel.getServiceStatus() == 1 ? R.drawable.shape_stroke_662c7df1_2dp : R.drawable.shape_stroke_669b9b9b_2dp);
                fragments.add(CompanyGsInfoFragment.newInstance(companyDetailsModel));
                fragments.add(CompanyServiceProgressFragment.newInstance(companyDetailsModel.getCreditNo()));
                fragments.add(CompanyInvoiceInfoFragment.newInstance(companyDetailsModel.getCreditNo()));
                // 第一种
                binding.viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
                binding.tabLayout.setTabData(mTabEntities);
                binding.viewPager.setOffscreenPageLimit(3);
                binding.viewPager.setCurrentItem(tabPos);
            }
        });

        viewModel.mErrorData.observe(this, ToastHelper::show);

        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
    }

}